﻿#include "DialogIPLCalculation.h"
#include "ui_DialogIPLCalculation.h"

#include "AnimationSettingData.h"
#include "DataChosenListDialog.h"
#include "LineChartWindow.h"
#include <QIntValidator>
#include "IdaWidgetHelper.h"
#include "IPLCalculationUIComponents.h"
#include "BaseWidget/IdaDialogMsg.h"
#include <QListView>
#include <QDebug>
#include "BaseModule/IBaseSignal.h"

namespace pst
{
    DialogIPLCalculation::DialogIPLCalculation(QWidget* parent)
        : IdaDialogFrame(parent), ui(new Ui::DialogIPLCalculation())
    {
        ui->setupUi(this);

        setWindowTitle(tr("IPL计算"));
        IdaWidgetHelperInstance->setMainButtonStyle(ui->pushButton_confirm); // 将确定设置为主按钮

        m_IPLCalculationUIComponentsPTT = new IPLCalculationUIComponents();
        m_IPLCalculationUIComponentsPTT->setUIComponentsName(QStringLiteral("测量试验中发生器输出功率"), QStringLiteral("P"), QStringLiteral("TT"), QStringLiteral("(dBm)"));
        ui->verticalLayout->addWidget(m_IPLCalculationUIComponentsPTT);

        m_IPLCalculationUIComponentsPMT = new IPLCalculationUIComponents();
        m_IPLCalculationUIComponentsPMT->setUIComponentsName(QStringLiteral("测量试验中接收信号功率"), QStringLiteral("P"), QStringLiteral("MT"), QStringLiteral("(dBm)"));
        ui->verticalLayout->addWidget(m_IPLCalculationUIComponentsPMT);

        m_IPLCalculationUIComponentsPTC = new IPLCalculationUIComponents();
        m_IPLCalculationUIComponentsPTC->setUIComponentsName(QStringLiteral("基准测量中发生器输出功率"), QStringLiteral("P"), QStringLiteral("TC"), QStringLiteral("(dBm)"));
        ui->verticalLayout->addWidget(m_IPLCalculationUIComponentsPTC);

        m_IPLCalculationUIComponentsPMC = new IPLCalculationUIComponents();
        m_IPLCalculationUIComponentsPMC->setUIComponentsName(QStringLiteral("基准测量中接收信号功率"), QStringLiteral("P"), QStringLiteral("MC"), QStringLiteral("(dBm)"));
        ui->verticalLayout->addWidget(m_IPLCalculationUIComponentsPMC);

        m_IPLCalculationUIComponentsGAA = new IPLCalculationUIComponents();
        m_IPLCalculationUIComponentsGAA->setUIComponentsName(QStringLiteral("有源天线放大器增益"), QStringLiteral("G"), QStringLiteral("AA"), QStringLiteral("(dB)"));
        ui->verticalLayout->addWidget(m_IPLCalculationUIComponentsGAA);

        m_IPLCalculationUIComponentsGAMP = new IPLCalculationUIComponents();
        m_IPLCalculationUIComponentsGAMP->setUIComponentsName(QStringLiteral("放大器增益"), QStringLiteral("G"), QStringLiteral("Amp"), QStringLiteral("(dB)"));
        ui->verticalLayout->addWidget(m_IPLCalculationUIComponentsGAMP);

        m_IPLCalculationUIComponentsILAtt = new IPLCalculationUIComponents();
        m_IPLCalculationUIComponentsILAtt->setUIComponentsName(QStringLiteral("衰减器插入损耗"), QStringLiteral("IL"), QStringLiteral("Att"), QStringLiteral("(dB)"));
        ui->verticalLayout->addWidget(m_IPLCalculationUIComponentsILAtt);

        m_IPLCalculationUIComponentsList.append(m_IPLCalculationUIComponentsPTT);
        m_IPLCalculationUIComponentsList.append(m_IPLCalculationUIComponentsPMT);
        m_IPLCalculationUIComponentsList.append(m_IPLCalculationUIComponentsPTC);
        m_IPLCalculationUIComponentsList.append(m_IPLCalculationUIComponentsPMC);
        m_IPLCalculationUIComponentsList.append(m_IPLCalculationUIComponentsGAA);
        m_IPLCalculationUIComponentsList.append(m_IPLCalculationUIComponentsGAMP);
        m_IPLCalculationUIComponentsList.append(m_IPLCalculationUIComponentsILAtt);
    }

    DialogIPLCalculation::~DialogIPLCalculation()
    {
        delete ui;
    }

    void DialogIPLCalculation::setCurveNameList(const QList<QString>& curveNameList)
    {
        m_curveNameList = curveNameList;
        setUIComponentsCureveNameList();
    }

    void DialogIPLCalculation::setInit(QStringList& params)
    {
        if (params.at(0) != "4")
            return;
        params.removeFirst();
        for (size_t i = 0; i < params.size(); i++)
        {
            m_IPLCalculationUIComponentsList.at(i)->setInit(params.at(i));
        }
    }

    void DialogIPLCalculation::setUIComponentsCureveNameList()
    {
        for (auto _uiComponent : m_IPLCalculationUIComponentsList)
            _uiComponent->setCurveNameList(m_curveNameList);
    }

    void DialogIPLCalculation::on_pushButton_confirm_clicked()
    {
        // 点确定后首先做错误判断
        auto _isOK = AbnormalErrorJudgment();
        if (!_isOK)
            return;
        // 在此之后做数据处理

        QList<QVariant> IPLDatas;
        QList<QString> pyData;
        for (auto _uiComponent : m_IPLCalculationUIComponentsList)
        {
            auto _radioButtonValueState = _uiComponent->getRadioButtonValueState();
            if (_radioButtonValueState)
            {
                IPLDatas.append(QVariant(_uiComponent->getIPLValue()));
                pyData.append(QString("DDOUBLE:%1").arg(_uiComponent->getIPLValue()));
            }
            else
            {
                IPLDatas.append(QVariant(m_curveNameList.at(_uiComponent->getIPLSeletedCurveIndexs().first())));
                pyData.append(QString("SSTRING:%1").arg(m_curveNameList.at(_uiComponent->getIPLSeletedCurveIndexs().first())));
            }
        }
        bool _success = true;
        emit signal_sendIPLDatas(IPLDatas, &_success);
        if (!_success)
            return;
        QString _pyCode = QString("PostProcessing.lineChartDialogIPLCalculation('%1')").arg(pyData.join(","));
        emit IBaseSignalInstance->signal_execPYCode(QStringList{_pyCode}, true, false);

        IdaDialogFrame::accept();
    }

    void DialogIPLCalculation::on_pushButton_cancel_clicked()
    {
        this->close();
    }

    bool DialogIPLCalculation::AbnormalErrorJudgment()
    {
        // 选择的曲线索引
        m_selectedIndexsPTT = m_IPLCalculationUIComponentsPTT->getIPLSeletedCurveIndexs();
        m_selectedIndexsPMT = m_IPLCalculationUIComponentsPMT->getIPLSeletedCurveIndexs();
        m_selectedIndexsPTC = m_IPLCalculationUIComponentsPTC->getIPLSeletedCurveIndexs();
        m_selectedIndexsPMC = m_IPLCalculationUIComponentsPMC->getIPLSeletedCurveIndexs();
        m_selectedIndexsGAA = m_IPLCalculationUIComponentsGAA->getIPLSeletedCurveIndexs();
        m_selectedIndexsGAMP = m_IPLCalculationUIComponentsGAMP->getIPLSeletedCurveIndexs();
        m_selectedIndexsILAtt = m_IPLCalculationUIComponentsILAtt->getIPLSeletedCurveIndexs();

        QString _text{};
        for (auto _uiComponent : m_IPLCalculationUIComponentsList)
        {
            auto _radioButtonValueState = _uiComponent->getRadioButtonValueState();
            if (_radioButtonValueState)
            {
                if (_uiComponent->getIPLValue() == -1)
                {
                    _text = _uiComponent->getUIComponentsNameAbbreviationLetter() + QString("参数不可为空！");
                    auto _res = IdaDialogMsg::showDialog(nullptr, _text, EDialogMsg::Warning, QStringLiteral("警告"), {QStringLiteral("确定")});
                    return false;
                }
            }
            else
            {
                if (_uiComponent->getIPLSeletedCurveIndexs().isEmpty())
                {
                    _text = _uiComponent->getUIComponentsNameAbbreviationLetter() + QString("曲线不能为空！");
                    auto _res = IdaDialogMsg::showDialog(nullptr, _text, EDialogMsg::Warning, QStringLiteral("警告"), {QStringLiteral("确定")});
                    return false;
                }
            }
        }

        // 需判断多条曲线有无交集范围，若无交集弹窗报错
        // if ()
        //{
        //     _text = QString("选择的多条曲线五交集范围，请重新选择曲线！");
        //     auto _res = IdaDialogMsg::showDialog(nullptr, _text, EDialogMsg::Warning, QStringLiteral("警告"), {QStringLiteral("确定")});
        //     return false;
        // }

        return true;
    }

} // namespace pst
